package com.example.statistics.ui.bank

import android.os.Bundle
import com.example.statistics.bean.Bank
import com.example.statistics.bean.BankFlowBean
import com.example.statistics.bean.DefineInputBean
import com.example.statistics.bean.MarketCustomerBean
import com.example.statistics.common.BaseInputActivity
import com.example.statistics.ex.getSync
import com.example.statistics.ex.loadingDialog
import com.example.statistics.ex.singleDialog
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.DefaultConfig
import com.example.statistics.weight.TextProcessingView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class BankFlowAddActivity : BaseInputActivity<BankFlowBean>() {
    var accountId: Int = 0
    var purposeList = listOf<String>() //用途
    var bankList: List<Bank>? = null
    var bank: Bank? = null
    var customerList: List<MarketCustomerBean>? = null

    override val title: String
        get() = "新增流水"

    override val url: String
        get() = UrlConfig.BANK_FLOW_ADD

    override val isDefault: Boolean
        get() = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val dialog = loadingDialog()
        accountId = intent.getIntExtra("accountId", 0)
        GlobalScope.launch {
            bankList = getSync<List<Bank>>(UrlConfig.BANK_ACCOUNT_LIST)
            customerList = getSync<List<MarketCustomerBean>>(UrlConfig.MARKET_CUSTOMER_LIST)
            bank = bankList?.find { it.ID == accountId }
            if (bank != null) {
                extentParams.add("accountId" to accountId)
            }
            withContext(Dispatchers.Main) {
                dialog.dismiss()
                initView()
                refresh()
            }
        }
    }


    override val inputDataList: List<DefineInputBean>
        get() {
            var numberString = ""
            val number = bank?.cardNumber?.replace(" ", "").orEmpty()
            if (number.length > 4) {
                numberString = number.substring(number.length - 4, number.length)
            }
            return listOf(
                DefineInputBean("日期:", "date", TextProcessingView.STYLE_DATE, defaultDateFormat = "yyyy-MM-dd"),
                DefineInputBean("时间:", "time", TextProcessingView.STYLE_DATE, defaultDateFormat = "HH:mm", labelValue = "00:00"),
                DefineInputBean("账户：", "account", TextProcessingView.STYLE_SELECTOR, bank?.bank + numberString, isNull = false,
                    singleDialogT = Pair(bankList.orEmpty()) { str, _ ->
                        val bank = bankList?.get(str)
                        bank?.let {
                            extentParams.add("accountId" to it.ID)
                        }
                    }
                ),
                DefineInputBean("金额:", "money", TextProcessingView.STYLE_INPUT_FLOAT,
                    afterTextChangeListener = { str, _ ->
                        extentParams.add("balance" to str?.toString().orEmpty())
                    }
                ),

                DefineInputBean("类型：", "type", TextProcessingView.STYLE_SELECTOR, isNull = false,
                    singleDialog = Pair(listOf("收入", "支出")) { str, list ->
                        purposeList = if (str == "收入") {
                            DefaultConfig.BANK_INCOME_LIST
                        } else {
                            DefaultConfig.BANK_EXPENSE_LIST
                        }
                    }
                ),
                DefineInputBean(
                    "用途：", "purpose", TextProcessingView.STYLE_SELECTOR, isNull = false,
                    clicked = {
                        singleDialog(*purposeList.toTypedArray()) {
                            this.contentText = it
                        }
                    },
                ),
                DefineInputBean("客户:", "customer", TextProcessingView.STYLE_SELECTOR,
                    singleDialogT = Pair(customerList.orEmpty()) { index, list ->
                        val customer = customerList?.get(index)
                        customer?.let {
                            extentParams.add("customerId" to it.ID)
                        }
                    }),
                DefineInputBean("备注:", "remark", TextProcessingView.STYLE_INPUT),
            )
        }


}